/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/14
 */
package cn.vpclub.admin.web.home;

import cn.vpclub.admin.entity.CommentEntity;
import cn.vpclub.admin.mapper.CommentMapper;
import cn.vpclub.admin.param.ArticleRequestParam;
import cn.vpclub.admin.param.CommentRequestParam;
import cn.vpclub.admin.util.BaseResponse;
import cn.vpclub.admin.util.ResponseCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "comment")
public class CommentController {
    @Autowired
    CommentMapper commentMapper;

    @RequestMapping(value = "addComment", method = RequestMethod.POST)
    public BaseResponse addComment(@RequestBody @Validated CommentRequestParam commentRequestParam, HttpServletRequest httpServletRequest) {
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setUserId((String) httpServletRequest.getAttribute("userId"));
        commentEntity.setArticleId(commentRequestParam.getArticleId());
        commentEntity.setCommentContent(commentRequestParam.getCommentContent());
        commentMapper.insert(commentEntity);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    @RequestMapping(value = "selectComment", method = RequestMethod.GET)
    public BaseResponse selectComment(@RequestParam String articleId) {
        List list = commentMapper.selectComment(articleId);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "selectCommentInfo", method = RequestMethod.POST)
    public BaseResponse selectCommentInfo(@Validated @RequestBody() ArticleRequestParam articleRequestParam) {
        List container = new ArrayList();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("article_id", articleRequestParam.getArticleId());
        queryWrapper.isNull("who_user");
        List<CommentEntity> list = commentMapper.selectList(queryWrapper);
        for (CommentEntity commentEntity : list) {
            System.out.println(commentEntity.getUserId());
        }
        Map map = new HashMap();
//        queryWrapper.select("user_id");
//        List list = commentMapper.selectCommentInfo(articleRequestParam.getArticleId());
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }
}
